package cha.app;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.util.Log;

/* this. refers to a Context object
 * 
 * The log can only be viewed using Logcat. You can run this from the android shell (adb)
 * or watch it using the Eclipse IDE with the Android plugin 
 * 
 * */

public class SensorTester extends Activity implements OnClickListener, OnItemClickListener, CustomSensorManagerListener{
  private static final String TAG = "TestApp";
  
  /*gui components*/
  private Button button1;
  private Button button2;
  private EditText input;
  private TextView label;
  private ListView lv_sensors;
  
  private ArrayList<String> sensor_list;
  private CustomSensorManager csm;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    Log.d( TAG, "Starting" );
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sensor_list = new ArrayList<String>();
    csm = new CustomSensorManager(getApplicationContext());
    
    /*initialize gui components */
    button1 = (Button)findViewById(R.id.button1);
    button2 = (Button)findViewById(R.id.button2);
    input = (EditText)findViewById(R.id.entry);
    label = (TextView)findViewById(R.id.label);
    lv_sensors = (ListView)findViewById(R.id.sensor_container);
    
    //get all the sensor names
    for(Sensor s : csm.getSensorList())
      sensor_list.add( s.getName() );

    //populate the gui list of sensors
    lv_sensors.setAdapter( new ArrayAdapter<String>(getApplicationContext(), R.layout.sensorlist, R.id.sensor_list, sensor_list) );
    lv_sensors.setDividerHeight( 1 );
    
    //add listners to components
    button1.setOnClickListener(this); 
    button2.setOnClickListener(this);
    lv_sensors.setOnItemClickListener(this);
  }
  
  //handle events caused by buttons
  public void onClick( View v ){
    if(v.equals(button1)){
      CharSequence output = 
        (input.getText().length() == 0 ? "No Text Entered" : input.getText());
      Toast.makeText(this, output, Toast.LENGTH_LONG).show(); //display a popup message at bottom of screen
    }
    else if(v.equals(button2)){
      label.setText( "WhoooOoooOOOa" );
      label.setTextColor( Color.BLACK );
      label.setBackgroundColor( Color.GREEN );
    }
  }

  //handle events caused by list views
  public void onItemClick( AdapterView<?> parent, View v, int position, long id ) {
    if(parent.equals(lv_sensors)){
      //grab the selected item
      String sensor_name = (String)lv_sensors.getItemAtPosition( position );
      Toast.makeText( getApplicationContext(), "Selected " + sensor_name, Toast.LENGTH_SHORT ).show();
      if(!csm.isActivated(sensor_name))
        csm.register(sensor_name);
      else
        csm.unregister(sensor_name);
    }
  }

  public void onSensorChange( SensorEvent event ) {
    
  }
}